package com.vf.admin.client.service.impl;

import com.vf.admin.client.dao.AppPriceSetDao;
import com.vf.admin.client.service.AppPriceSetService;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppPriceSet;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AppPriceSetServiceImpl implements AppPriceSetService {

    @Autowired
    AppPriceSetDao apppricese;

    @Override
    public List<AppPriceSet> page(Map<String, Object> params, SplitPageBean splitPage) {
        return apppricese.page(params, splitPage);
    }

    @Override
    public void save(AppPriceSet appPriceSet) {
        if (appPriceSet.getId() != null && appPriceSet.getId() > 0) {
            apppricese.update(appPriceSet);
        } else {
            apppricese.add(appPriceSet);
        }
    }

    @Override
    public AppPriceSet info(Integer id) {
        return apppricese.info(id);
    }

    @Override
    public void del(Integer id) {
        apppricese.del(id);
    }

    @Override
    public Map<String, List<AppPriceSet>> typeList() {
        List<AppPriceSet> list = apppricese.typeList();
        Map<String, List<AppPriceSet>> listMap = list.stream().collect(Collectors.groupingBy(e -> e.getType().toString()));

        for (Map.Entry<String, List<AppPriceSet>> entry : listMap.entrySet()) {
            List<AppPriceSet> mapList = entry.getValue().stream().sorted(Comparator.comparing(e -> Integer.valueOf(e.getPrice()))).collect(Collectors.toList());
            mapList.stream().forEach(e -> {
                e.setName();
            });
            entry.setValue(mapList);
        }

        return listMap;
    }

    @Override
    public Map<String, String> maxPriceNoCharm() {
        Map<String, String> map = new HashMap<>();
        AppPriceSet appPriceSet1 = apppricese.maxPriceNoCharm(1);
        AppPriceSet appPriceSet2 = apppricese.maxPriceNoCharm(2);
        AppPriceSet appPriceSet3 = apppricese.maxPriceNoCharm(3);

        map.put("message", StringUtils.isNotBlank(appPriceSet1.getPrice()) ? appPriceSet1.getPrice() : "0");
        map.put("voice", StringUtils.isNotBlank(appPriceSet2.getPrice()) ? appPriceSet2.getPrice() : "0");
        map.put("video", StringUtils.isNotBlank(appPriceSet3.getPrice()) ? appPriceSet3.getPrice() : "0");
        return map;
    }

    @Override
    public Map<String, String> lowPriceNoCharm() {
        Map<String, String> map = new HashMap<>();
        AppPriceSet appPriceSet1 = apppricese.lowPriceNoCharm(1);
        AppPriceSet appPriceSet2 = apppricese.lowPriceNoCharm(2);
        AppPriceSet appPriceSet3 = apppricese.lowPriceNoCharm(3);

        map.put("message", StringUtils.isNotBlank(appPriceSet1.getPrice()) ? appPriceSet1.getPrice() : "0");
        map.put("voice", StringUtils.isNotBlank(appPriceSet2.getPrice()) ? appPriceSet2.getPrice() : "0");
        map.put("video", StringUtils.isNotBlank(appPriceSet3.getPrice()) ? appPriceSet3.getPrice() : "0");
        return map;
    }

    @Override
    public AppPriceSet infoByTypeAndPrice(Integer type, String price) {
        return apppricese.infoByTypeAndPrice(type, price);
    }
}
